/**
 * Created by DELL on 2016/8/8.
 */
var subAttrListModel = {
    heading:{
        "序号":"50px",
        "属性名称":"80px",
        "属性值":'60px',
        "显示名称":'80px',
        "所属属性":'80px',
        "编辑":'60px',
        "删除":'60px'
    },
    caption:"子属性列表",
    data:[{'subAttrNo':'1','subName':'device_flow_period','subValue':'3000','subShowName':'家电流量周期',//'subAttrNo':'1'
        'parentAttr':'更新周期'}, {'subAttrNo':'2','subName':'cloud_flow_period','subValue':'3000','subShowName':'云端流量周期',//'subAttrNo':'2'
            'parentAttr':'更新周期'}, {'subAttrNo':'3','subName':'device_exp_priod','subValue':'3000','subShowName':'家电指数周期',//'subAttrNo':'3'
            'parentAttr':'更新周期'}],
    attr:["","attr_id","attr_name","attr_value","attr_text","master_name"],
    parentAttr:null,
    init:function () {
        var oTable = document.getElementById('subAttr');
        var oTrList = oTable.getElementsByTagName("tr");
        for(var i = oTrList.length-1;i>0;i--){
            oTrList[i].remove();
        }
        var caption= oTable.getElementsByTagName('caption');
        if(caption.length != 0){
            oTable.removeChild(caption[0]);
        }

        buildTable.init('subAttr',subAttrListModel.caption,subAttrListModel.heading);

       //this.addSubAttr(data);..............
        //添加按钮
        var addSubAttrBtn = document.getElementById('addSubAttrBtn');
        addSubAttrBtn.onclick=function () {
            var oForm = document.getElementById('newSubAttr');

            oForm.reset();
            var parent_attr= document.getElementById('parent_attr');
            parent_attr.value = subAttrListModel.parentAttr;
            var input = document.getElementById('attr_id');
            input.value = -1;
            indexModel.showDiv('subAttrEditDetail');
        }
        var back = document.getElementById('backMain2');
        //alert("back");
        back.onclick = function () {
            //alert("back");
            indexModel.showDiv('mainAttrListDetail');

        }
    },
    run:function () {
        var data = subAttrListModel.data;
        for(var i = 0;i < data.length;i++){
            subAttrListModel.addSubTr(data[i]);
        }
        indexModel.showDiv('subAttrListDetail');//////
    },

    addSubAttr:function (data) {
        var attr = subAttrListModel.attr;
        var oData = "{";
        for(var i = 1;i < 6;i++){
            oData +="'"+attr[i]+"':'"+data[attr[i]]+"',";
        }
        oData+="}";
        data = eval("("+oData+")");
        buildTable.addTr('subAttr',data);
        var table = document.getElementById('subAttr');
        var trList = table.getElementsByTagName('tr');
        //alert(trList.length);
        subAttrListModel.addBtn(trList.length-1);


    },
    addBtn:function (i) {
        var oTable = document.getElementById('subAttr');
        var oTrList = oTable.getElementsByTagName("tr");
        var oTd = document.createElement('td');
        var oBtn = buildTable.createBtn('编辑',function () {
            document.getElementById('attr_name').disabled = true;
            var dataJson = subAttrListModel.getAttrTr(this);
            console.log('attr_name:'+dataJson['attr_name']);
            for(var i in dataJson){
                var oInput = document.getElementById(i);
                oInput.value = dataJson[i];
            }
            var parent_attr= document.getElementById('parent_attr');
            parent_attr.value = dataJson['master_name'];
            indexModel.showDiv('subAttrEditDetail');
        });
        oTd.appendChild(oBtn);
        oTrList[i].appendChild(oTd);
        oTd = document.createElement('td');
        oBtn = buildTable.createBtn('删除',function () {
            var dataJson = subAttrListModel.getAttrTr(this);

            var attr_name= dataJson['attr_name'];
            var master_name= dataJson['master_name'];
            console.log('delete attr_name:'+dataJson['attr_name']+",master_name:"+master_name);
            var flag = confirm('是否确定删除?');
            if(flag){
                //删除操作   。。。。
                subAttrListModel.deleteAttr(attr_name,master_name);
                // var a= $(this).parent().parent().remove();
            }
        })
        oTd.appendChild(oBtn);
        oTrList[i].appendChild(oTd);
    },
    addSubTr:function (data) {//添加一行新的子属性
        var attr = subAttrListModel.attr;
        var sData = "{";
        for(var i= 1;i < attr.length;i++){
            sData=sData+"'"+attr[i]+"':'"+data[attr[i]]+"',";
        }
        sData=sData+"}";
        //alert(sData);
        data = eval("("+sData+")");
        buildTable.addTr('subAttr',data);
        var table = document.getElementById('subAttr');
        var trlist = table.getElementsByTagName('tr');
        subAttrListModel.addBtn(trlist.length-1);

    },
    showSubList:function (master_name) {

        subAttrListModel.init();
        subAttrListModel.parentAttr = master_name;
        $.ajax({
            type:"get",
            url: indexModel.url+"detail/list",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"data1",
            data :"master_name="+master_name,
            success : function(json) {
                //alert(json);
                subAttrListModel.formatData(json);
                subAttrListModel.run();
                indexModel.showDiv('subAttrListDetail');
            },
            error: function() {
               // console.log("tbl_getAttackSource没有值!");
               // alert("show subList 查询数据库失败!");
            }
        });

    },
    formatData:function (data) {
        data.reverse();
        for(var i = 0;i < data.length;i++){
            data[i]['attr_id'] = i+1;
            data[i]['parent_attr'] = subAttrListModel.parentAttr;
            data[i]['master_name'] = subAttrListModel.parentAttr;
        }
        subAttrListModel.data = data;
        return data;
    },
    getAttrTr:function (btn) {
        var pTr = $(btn).parent().parent().html();
        var data = pTr.split("<td>");

        var oData="{";
        var attr =subAttrListModel.attr;
        for(var i= 1;i < 6;i++){
            data[i]= data[i].replace("</td>","");
            oData+="'"+attr[i]+"':'"+data[i]+"',";
        }
        oData+="}";
        // alert(oData);
        var oFrom = document.getElementById('newSubAttr');
        var child = oFrom.getElementsByClassName('input1');
        // alert(child.length);
        var dataJson = eval("("+oData+")");
        return dataJson;
    },
    deleteAttr:function (attr_name,master_name) {
        $.ajax({
            type:"get",
            url: indexModel.url+"detail/delete",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"data1",
            data :"attr_name="+attr_name,
            success : function(json) {
                //alert(json);
                alert('删除成功!');
                subAttrListModel.showSubList(master_name);
            },
            error: function() {
                // console.log("tbl_getAttackSource没有值!");
                alert("show subList 查询数据库失败!");
            }
        });

    }

}